package com.shujia.flink.sink

import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.sink.{RichSinkFunction, SinkFunction}
import org.apache.flink.streaming.api.scala._
import redis.clients.jedis.Jedis

object Demo5RedisSInk {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    //统计单词的数量
    val countDS: DataStream[(String, Int)] = linesDS
      .flatMap(_.split(","))
      .map((_, 1))
      .keyBy(_._1)
      .sum(1)

    //将解雇我保存到redis中
    countDS.addSink(new RichSinkFunction[(String, Int)] {
      var jedis: Jedis = _

      override def open(parameters: Configuration): Unit = {
        //创建redis连接
        jedis = new Jedis("master", 6379)
      }

      override def close(): Unit = {
        jedis.close()
      }

      override def invoke(value: (String, Int), context: SinkFunction.Context): Unit = {
        val word: String = value._1
        val count: Int = value._2
        //将数据保存到redis中
        jedis.set(s"word_count:$word", count.toString)
      }
    })

    env.execute()

  }

}
